const app = getApp();
const util = require('../../utils/util.js')
Page({
    data: {
        imgUrl: app.globalData.imgUrl,
        equipmentId: '',
        equip_percents: [{
            user: '',
            name: '',
            percent: ''
        }, {
            user: '',
            name: '',
            percent: ''
        }],
        selfPercent: '',
        userInfo: {},

    },
    onShow: function () {
        console.log(this.data.equip_percents)
    },
    onLoad: function (option) {
        wx.setNavigationBarTitle({
            title: '设置设备销售分成'
        })
        this.setData({
            equipmentId: option.equipmentId,
            selfPercent: 100 - this.data.equip_percents[0].percent - this.data.equip_percents[1].percent,
            userInfo: wx.getStorageSync('userInfo')
        })
    },

    inputPercent: function (e) {
        var param = {};
        var percent = 'equip_percents[' + e.currentTarget.dataset.name + '].percent'
        param[percent] = e.detail.value;
        this.setData(param);
        this.setData({
            selfPercent: 100 - this.data.equip_percents[0].percent - this.data.equip_percents[1].percent
        })
        if (this.data.selfPercent < 0) {
            wx.showModal({
                title: '分成总数不能大于100%'
            })
        }

    },
    addPercetn: function(){
        var equip_percents = this.data.equip_percents;

        var checkData = equip_percents.map(function (item,i) {
            if(item.percent && !item.user) {
                wx.showModal({
                    title: '错误：分成比例'+ item.percent + '%，未指定用户'
                })
                return item;
            } else if(!item.percent && item.user){
                wx.showModal({
                    title:'错误：用户'+ item.name +'，未设置分成'
                })
                return item;
            } else {
                return item;
            }

        });
        var newmap = checkData.filter(function (item) {
            return (item.percent || item.user);
        })

        var isReady =newmap.every(function (item) {
            return (item.percent && item.user)
        })
        console.log(isReady);
        if(isReady){
            this.savePercent(newmap);
        }


    },
    savePercent: function (equip_percents) {
        wx.showLoading({
            title: '请稍候'
        })
        var paramObj = {};
        var selfPercent = {
            user: this.data.userInfo.id,
            percent: this.data.selfPercent
        }
        //避免重复push
        function unique(arr) {
            var res = [];
            for(var item of arr){
                if(item.user !== selfPercent.user){
                    res.push(item);
                }
            }
            return res;
        }
        var newPercent = unique(equip_percents);
        newPercent.push(selfPercent);
        paramObj.equip_percents = newPercent;
        console.log(paramObj)

        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/addPercent',
            // url: 'http://192.168.90.189:8080/vending-admin/active/addPercent',
            data: {
                token: app.globalData.token,
                equipment_id: that.data.equipmentId,
                paras: paramObj,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showToast({
                        title: '设置完成',
                        icon: 'success',
                        duration: 3000
                    });
                    let pages = getCurrentPages();
                    let prevPage = pages[pages.length - 2];

                    prevPage.setData({
                        isPercentSet: true
                    });
                    wx.navigateBack({
                        delta: 1
                    })

                }
            },
            fail: function (err) {
                console.log(err)
            }
        })
    }


});